# update_data.py
import akshare as ak
import pandas as pd
from datetime import datetime

def fetch_and_save_fund_data():
    print("正在获取基金数据...")
    try:
        fund_data = ak.fund_open_fund_daily_em()
        # 添加时间戳
        fund_data['update_time'] = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        # 保存到 CSV
        fund_data.to_csv('data/fund_data.csv', index=False, encoding='utf-8')
        print(f"数据已保存，共 {len(fund_data)} 条记录。")
    except Exception as e:
        print("获取数据失败:", e)

if __name__ == '__main__':
    fetch_and_save_fund_data()